import {
	baseUrl,
	baseApiUrl
} from './config.js';
import * as common from './common.js' //引入common
import * as db from './db.js' //引入common
// 需要登陆的，都写到这里，否则就是不需要登陆的接口
const methodsToken = [
	'refreshUser',
	'getMyData',
	'logout',
	'setReviews',
	'profile',
	'changepwd',
	'changeMobile',
	'sendXcxMessage',
	'getMyCouponCount',
	'getMyCouponLists',
	'clinicLists',
	'verifyCoupon',
	'couponDetails',
	'getMyData',
	'createAutoCouponlists',
	'getVerifyCoupon',
	'getClinicLists',
	'getCouponDetails',
	'getMyClinicInfo',
	'getSubAppointment',
	'getMyAppointment',
	'getOrders',
	'getClinicStaff',
	'getverifyStaff',
	'getOneAppointment',
	'subClinicApply',
	'isClinicApply',
	'couponProfile',
	'getCouponProfile',
	// 'findNoUserClinic',
  'bindMyClinic',
	'setClinicPassword'
];
const post = (method, data, callback,type) => {
	let userToken = '';
	let auth = '';
	// 判断token是否存在
	if (methodsToken.indexOf(method) >= 0) {
		// 获取用户token
		let auth = db.get("auth");
		//console.log(auth);
		let nowdate = (new Date()) / 1000; //当前时间戳
		//新增用户判断是否登录逻辑begin
		 common.isLogin();
		//新增用户判断是否登录逻辑end
		if (!auth || auth.createtime+auth.expires_in < nowdate) {
			common.toLogin();
			return false;
		} else {
			userToken = auth.token;
		}
	}
	
	if(type){
		method =  type + '/' + method
	}else{
		method = '/' + method
	}
    uni.showLoading({title:'努力加载中',icon:'loading'});
	uni.request({
		url: baseApiUrl + method,
		data: data,
		header: {
			'Accept': 'application/json',
			'Content-Type': 'application/x-www-form-urlencoded',
			'token': userToken,
		},
		method: 'POST',
		success: (response) => {
			uni.hideLoading();
			const result = response.data
			if (result.msg == 'Please login' || result.msg == '请登陆') {
				db.del("user");
				db.del("auth");
				console.log('未登陆')
				uni.showToast({
					title: result.msg,
					icon: 'none',
					duration: 2000,
					complete: function() {
						uni.reLaunch({
							url: '/pages/index/index',
						})
					}
				});
			}
			callback(result);
		},
		fail: (error) => {
			uni.hideLoading();
			if (error && error.response) {
				showError(error.response);
			}
		},
	});

}


// 上传图片
export const uploadImage = (method , data = {} , callback , num = 9 ,type) => {
	if(type){
		method =  type + '/' + method
	}else{
		method =method
	}
	let userToken = '';
	let auth = db.get("auth");
	userToken = auth.token;
	uni.chooseImage({
		count:num,
		success: (res) => {
			uni.showLoading({
				title: '上传中...'
			});
			let tempFilePaths = res.tempFilePaths
			for (var i = 0; i < tempFilePaths.length; i++) {
				data.file = tempFilePaths[i]
				uni.uploadFile({
					url: baseApiUrl + method,
					filePath: tempFilePaths[i],
					fileType: 'image',
					name: 'file',
					headers: {
						'Accept': 'application/json',
						'Content-Type': 'multipart/form-data',
						'token': userToken
					},
					formData: data,
					success: (uploadFileRes) => {
						callback(JSON.parse(uploadFileRes.data))
					},
					fail: (error) => {
						if (error && error.response) {
							common.showError(error.response);
						}
					},
					complete: () => {
						setTimeout(function () {
							uni.hideLoading();
						}, 250);
					},
				});
			}
		}
	});
}

const get = (url, callback) => {
	uni.showLoading({
		title: '加载中'
	});
	uni.request({
		url: url,
		header: {
			'Accept': 'application/json',
			'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
		},
		method: 'GET',
		success: (response) => {
			callback(response.data);
		},
		fail: (error) => {
			if (error && error.response) {
				showError(error.response);
			}
		},
		complete: () => {
			setTimeout(function() {
				uni.hideLoading();
			}, 250);
		}
	});
}

const showError = error => {
	let errorMsg = ''
	switch (error.status) {
		case 400:
			errorMsg = '请求参数错误'
			break
		case 401:
			errorMsg = '未授权，请登录'
			break
		case 403:
			errorMsg = '跨域拒绝访问'
			break
		case 404:
			errorMsg = `请求地址出错: ${error.config.url}`
			break
		case 408:
			errorMsg = '请求超时'
			break
		case 500:
			errorMsg = '服务器内部错误'
			break
		case 501:
			errorMsg = '服务未实现'
			break
		case 502:
			errorMsg = '网关错误'
			break
		case 503:
			errorMsg = '服务不可用'
			break
		case 504:
			errorMsg = '网关超时'
			break
		case 505:
			errorMsg = 'HTTP版本不受支持'
			break
		default:
			errorMsg = error.msg
			break
	}
	uni.showToast({
		title: errorMsg,
		icon: 'none',
		duration: 2000
	});
}
// 密码登录
export const login = (data, callback) => post('login', data, callback,'coupons/User');
// 注册
export const register = (data, callback) => post('register', data, callback,'coupons/User');
// 登录
export const third = (data, callback) => post('third', data, callback,'coupons/User');
// 用户绑定手机
export const bindphone = (data, callback) => post('bind', data, callback,'coupons/User');
// 修改用户信息
export const profile = (data, callback) => post('profile', data, callback,'coupons/User');
// 发送验证码
export const sendSmsVerify = (data, callback) => post('sendSmsVerify', data, callback,'coupons/User');
// 刷新用户
export const refreshUser = (data, callback) => post('refreshUser', data, callback,'coupons/User');
// 上传头像
export const upload = (data, callback) => post('upload', data, callback,'common');

// 绑定手机
export const changeMobile = (data, callback) => post('changeMobile', data, callback,'coupons/User');
// 获取我的用户预约+订单数据
export const getMyData = (data, callback) => post('getMyData', data, callback,'coupons/User');
// 我的优惠券列表 /api/coupon.User/getMyCouponLists
export const getMyCouponLists = (data, callback) => post('getMyCouponLists', data, callback,'coupons/User');
// 优惠券详情 /api/coupon.user/couponDetails
export const getCouponDetails = (data, callback) => post('getCouponDetails', data, callback,'coupons/User');
//  获取我的优惠券数量 /api/coupon.user/getMyCouponCount
export const getMyCouponCount = (data, callback) => post('getMyCouponCount', data, callback,'coupons/User'); 
//  门店--列表 /api/coupon.user/getClinicLists
export const getClinicLists = (data, callback) => post('getClinicLists', data, callback,'coupons/User'); 

// 门店人员核销优惠券子方法 /api/coupon.user/getVerifyCoupon
export const getVerifyCoupon = (data, callback) => post('getVerifyCoupon', data, callback,'coupons/User');
// 三方接口,自动生成优惠券模块 /api/coupon.user/createAutoCouponlists
export const createAutoCouponlists = (data, callback) => post('createAutoCouponlists', data, callback,'coupons/User');
// 门店个人中心+用户+数据 /api/coupon.user/getMyClinicInfo
export const getMyClinicInfo = (data, callback) => post('getMyClinicInfo', data, callback,'coupons/User');
// 提交/修改预约门店信息 /api/coupon.user/getSubAppointment
export const getSubAppointment = (data, callback) => post('getSubAppointment', data, callback,'coupons/User');
// 获取我的预约列表 /api/coupon.user/getMyAppointment
export const getMyAppointment = (data, callback) => post('getMyAppointment', data, callback,'coupons/User');
// 获取门店订单列表 /api/coupon.user/getOrders
export const getOrders = (data, callback) => post('getOrders', data, callback,'coupons/User');
// 获取门店员工列表 /api/coupon.user/getClinicStaff
export const getClinicStaff = (data, callback) => post('getClinicStaff', data, callback,'coupons/User');
// 审核员工加入门店 /api/coupon.user/getverifyStaff
export const getverifyStaff = (data, callback) => post('getverifyStaff', data, callback,'coupons/User');

// 预约信息详情 /api/coupon.user/getOneAppointment
export const getOneAppointment = (data, callback) => post('getOneAppointment', data, callback,'coupons/User');

// 扫码加入门店 /api/coupon.user/getOneAppointment
export const subClinicApply = (data, callback) => post('subClinicApply', data, callback,'coupons/User');

// 员工扫码验证是否已经提交以及提交状态 /api/coupon.user/getOneAppointment
export const isClinicApply = (data, callback) => post('isClinicApply', data, callback,'coupons/User');

// 编辑门店信息 /api/coupon.user/getOneAppointment
export const couponProfile = (data, callback) => post('couponProfile', data, callback,'coupons/User');

// 编辑门店原有信息 /api/coupon.user/getOneAppointment
export const getCouponProfile = (data, callback) => post('getCouponProfile', data, callback,'coupons/User');
 
//门店关键字
// export const findNoUserClinic = (data, callback) => post('findNoUserClinic', data, callback,'coupons/User');

//绑定门店管理员
export const bindMyClinic = (data, callback) => post('bindMyClinic', data, callback,'coupons/User');

//修改门店密码
export const setClinicPassword = (data, callback) => post('setClinicPassword', data, callback,'coupons/User');

 
 
 
 
 
 